package www.openteam.com.model.business;

import www.openteam.com.OTApplication;
import www.openteam.com.base.BaseModel;
import www.openteam.com.bean.CustomType;
import www.openteam.com.bean.InsertStatus;
import www.openteam.com.bean.ProductInfo;
import www.openteam.com.exception.ApiException;
import www.openteam.com.subscribe.CommonSubscriber;
import www.openteam.com.transformer.CommonTransformer;

/**
 * Created by hyd on 2016/10/16.
 */

public class EditProductInfoModel extends BaseModel {

    private boolean isQuery = false;
    private boolean isQueryM = false;
    private boolean isDelete = false;

    public boolean queryMaterialTypelst(final EditProductMInfoOnListener editProductInfoOnListener) {

        httpApiService.queryMaterialTypelst()
                .compose(new CommonTransformer<CustomType>())
                .subscribe(new CommonSubscriber<CustomType>(OTApplication.getmContext()) {
                    @Override
                    public void onNext(CustomType customType) {
                        isQuery = true;
                        editProductInfoOnListener.queryMTSuccess(customType);
                    }

                    @Override
                    protected void onError(ApiException e) {
                        super.onError(e);

                        isQuery = false;
                        editProductInfoOnListener.queryMTFail(e);
                    }
                });

        return isQuery;
    }


    public boolean queryProductInfo(String code, String name, int PageSize, final EditProductPInfoOnListener editProductInfoOnListener) {
        httpApiService.queryProductInfo(code, name, PageSize)
                .compose(new CommonTransformer<ProductInfo>())
                .subscribe(new CommonSubscriber<ProductInfo>(OTApplication.getmContext()) {
                    @Override
                    public void onNext(ProductInfo productInfo) {
                        isQueryM = true;
                        editProductInfoOnListener.queryPISuccess(productInfo);
                    }

                    @Override
                    protected void onError(ApiException e) {
                        isQueryM = false;
                        editProductInfoOnListener.queryPIFail(e);

                    }
                });
        return isQueryM;
    }

    public boolean deleteProductInfo(String code, final EditProductDInfoOnListener editProductInfoOnListener) {
        httpApiService.deleteProductInfo(code)
                .compose(new CommonTransformer<InsertStatus>())
                .subscribe(new CommonSubscriber<InsertStatus>(OTApplication.getmContext()) {
                    @Override
                    public void onNext(InsertStatus insertStatus) {
                        isDelete = true;
                        editProductInfoOnListener.deletePISuccess(insertStatus);
                    }

                    @Override
                    protected void onError(ApiException e) {
                        super.onError(e);
                        isDelete = false;
                        editProductInfoOnListener.deletePIFail(e);
                    }
                });


        return isDelete;

    }


    public interface EditProductMInfoOnListener {
        void queryMTSuccess(CustomType materialTypelst);

        void queryMTFail(Throwable throwable);


    }
    public interface EditProductPInfoOnListener {
        void queryPISuccess(ProductInfo productInfo);

        void queryPIFail(Throwable throwable);

    }
    public interface EditProductDInfoOnListener {
        void deletePISuccess(InsertStatus insertStatus);

        void deletePIFail(Throwable throwable);

    }



}
